<template>
  <div class="tree-item">
    <span class="i1">{{ item.name }}</span>
    <span class="i2">{{ item.manager }}</span>
    <el-dropdown class="i3" @command="commandEvent">
      <span>
        <span>操作</span>
        <i class="el-icon-arrow-down el-icon--right" />
      </span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item command="add">添加子部门</el-dropdown-item>
          <el-dropdown-item
            v-if="!item.isTop"
            command="look"
          >查看部门</el-dropdown-item>
          <el-dropdown-item
            v-if="!item.isTop"
            command="del"
          >删除部门</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script>
import { companyDepartmentDel } from '@/api/departments'
export default {
  name: 'TreeItem',
  props: ['item'],
  methods: {
    async commandEvent(command) {
      switch (command) {
        // 新增部门
        case 'add':
          this.$bus.$emit('show', true, this.item)
          break
        // 查看部门
        case 'look':
          this.$bus.$emit('show', true, this.item, 'edit')
          break
        // 删除部门
        case 'del':
          this.$confirm('确定要删除吗', '删除部门')
            .then(async() => {
              await companyDepartmentDel(this.item.id)
              this.$message.success('删除部门成功')
              this.$emit('getData')
            })
            .catch(() => {})
          break
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-item {
  display: flex;
  align-items: center;

  .i1 {
    flex: 1;
    font-weight: 700;
  }
  .i2,
  .i3 {
    width: 150px;
    text-align: right;
  }
}
</style>
